package de.velvettear.squirrel.adapter;

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.kernel.AccessPoint;
import de.velvettear.squirrel.kernel.Statics;

public class AccessPointAdapter extends ArrayAdapter {
	
	private Context context;
	
	private Vector<AccessPoint> aps;
				
	public AccessPointAdapter(Context context, int layout, int textViewResourceId, Vector<AccessPoint> aps) {
		super(context, layout, textViewResourceId, aps);
		this.context = context;
		this.aps = aps;
	}
	
	@Override
	public int getCount() {
		return aps.size();
	}

	@Override
	public AccessPoint getItem(int position) {
		return aps.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		AccessPointViewHolder viewHolder;
		AccessPoint ap = aps.get(position);		
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.listview_accesspoints, null);
			
			viewHolder = new AccessPointViewHolder();
			viewHolder.tv_bssid = (TextView) convertView.findViewById(R.id.listview_accesspoints_bssid);	
			viewHolder.tv_capabilities = (TextView) convertView.findViewById(R.id.listview_accesspoints_capabilities);
			viewHolder.tv_ssid = (TextView) convertView.findViewById(R.id.listview_accesspoints_ssid);
			viewHolder.tv_frequency = (TextView) convertView.findViewById(R.id.listview_accesspoints_frequency);
			viewHolder.iv_level = (ImageView) convertView.findViewById(R.id.listview_accesspoints_level);
			
			viewHolder.icon_bad = convertView.getResources().getDrawable(R.drawable.icon_signal_bad);
			viewHolder.icon_low = convertView.getResources().getDrawable(R.drawable.icon_signal_low);
			viewHolder.icon_good = convertView.getResources().getDrawable(R.drawable.icon_signal_good);
			viewHolder.icon_excellent = convertView.getResources().getDrawable(R.drawable.icon_signal_excellent);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (AccessPointViewHolder) convertView.getTag();
		}		
				
		viewHolder.tv_bssid.setText(ap.m_sBSSID);	
		viewHolder.tv_capabilities.setText(ap.m_sCapabilities);
		viewHolder.tv_ssid.setText(ap.m_sSSID);				
		viewHolder.tv_frequency.setText(ap.m_nFrequency + " MHz");
		
		String signalStrength = ap.getSignalStrength();
		if (signalStrength.equals(Statics.SIGNAL_BAD)) {
			viewHolder.iv_level.setImageDrawable(viewHolder.icon_bad);
		} else if (signalStrength.equals(Statics.SIGNAL_LOW)) {
			viewHolder.iv_level.setImageDrawable(viewHolder.icon_low);
		} else if (signalStrength.equals(Statics.SIGNAL_GOOD)) {
			viewHolder.iv_level.setImageDrawable(viewHolder.icon_good);
		} else if (signalStrength.equals(Statics.SIGNAL_EXCELLENT)) {
			viewHolder.iv_level.setImageDrawable(viewHolder.icon_excellent);
		}
		
		return convertView;	
	}
	
	static class AccessPointViewHolder {
		View convertView;
		TextView tv_bssid;
		TextView tv_capabilities;
		TextView tv_ssid;
		TextView tv_frequency;
		ImageView iv_level;
		Drawable icon_bad;
		Drawable icon_low;
		Drawable icon_good;
		Drawable icon_excellent;
	}
	
}
